package com.qlylkj.lbk.network


import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import com.forest.core.etx.connectivityManager
import com.jeremyliao.liveeventbus.LiveEventBus
import com.qlylkj.lbk.app.PreferenceHelper
import com.qlylkj.lbk.model.event.LiveEvent


class NetworkChangeReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        context?.run {
            intent?.run {
                val isConnected = connectivityManager?.activeNetworkInfo?.isConnected ?: false
                if (isConnected) {
                    if (!PreferenceHelper.hasNetwork) {
                        LiveEventBus.get(LiveEvent.NETWORK_CHANGE_KEY).post(isConnected)
                    }
                } else {
                    LiveEventBus.get(LiveEvent.NETWORK_CHANGE_KEY).post(isConnected)
                }
            }
        }
    }
}